<html>
<head>
<link rel="Stylesheet" type="text/css" href="../../DocStyle.css" />
<title>Vczh Library++ Basic</title>
</head>
<body>
<h1>namespace vl;</h1>
<h2><a name="NotCopyable">NotCopyable</a></h2>
<p>NotCopyable class is a empty class. The only way to use NotCopyable is inheriting it. Any class that inherits NotCopyable can not be copied unless the class defines a copy constructor or an operator= overloading.</p>
<p>NotCopyable is a concept, but it is not a type for abstraction. Using a private inheriting is expected to create a uncopyable sub class.</p>
<table id="code"><tr><td><pre>
#include "Library\Basic.h"
using namespace vl;

class MyUncopyableClass : public Object, private NotCopyable
{
public:
    // no copy constructor and operator= overloading
};
</pre></td></tr></table>
<h2><a name="Error">Error</a></h2>
<p>Error class is a kind of exceptions which is used to point out some programming mistakes. It is different from Exception. </p>
<p>To use error, you should use the macro CHECK_ERROR(condition, description) instead of directly throwing an instance. CHECK_ERROR will throw an instance of Error when the condition is evaluated to false. Description should be a unicode string constant like L"Hey!, I am an Error.". CHECK_ERROR is something like ASSERT.</p>
<table id="code"><tr><td><pre>
#include "Library\Basic.h"
using namespace vl;

bool itob(int boolean)
{
    CHECK_ERROR(boolean==0 || boolean==1, L"The value of boolean should be 0 or 1.");
    return boolean==1;
}
</pre></td></tr></table>
<h2><a name="Object">Object</a></h2>
<p>Object class is the base class of almost every class in the core library. The benefit to inherit from Object is that Object has a virtual destructor. And inheriting from Object make it possible to store classes in List&lt;Ptr&lt;Object&gt;&gt; or some similar ways.</p>
<p>If you define a class without directly or indirectly inherits Object, you may not safe to cast it to one of the base class if the base class does not contains a virtual destructor.</p>
<table id="code"><tr><td><pre>
#include "Library\Basic.h"
using namespace vl;

class MyClass : public Object
{
};
</pre></td></tr></table>
<h2><a name="Exception">Exception</a></h2>
<p>Exception class is a kind of exceptions which is used to control the flow of the code. Exception should only be used when it is difficult to do the same job without it such as jumping out of a recursive-descent parser.</p>
<p>Exception class has only one method: WString Exception::Message(); (<i>It was called GetMessage and I was hurt because I include the windows.h in another project</i>). After an instance of Exception is created, the message included in it can not be changed. The only way to provide a message is to put it into the constructor. If you do not provide a message when you are constructing an Exception, the WString::Empty is used for it.</p>
<p>ArgumentException and ParsingException are provided with Exception in Library\Exception.h for different use. Most parts of the core library do not use Exception and its sub classes as part of the public interface.</p>
<table id="code"><tr><td><pre>
#include "Library\Basic.h"
#include "Library\Exception.h"
using namespace vl;

class LoginException : public Exception
{
public:
    LoginException(const WString& message)
        :Exception(message)
    {
    }
};

void Login(const WString& name, const WString& password)
{
    if(IsWrong(name, password))
    {
        throw LoginException(L"Wrong name or password.");
    }
}

int main()
{
    try
    {
        Login(L"vczh", L"genius");
    }
    catch(const LoginException& e)
    {
        Console::WriteLine(L"Error occured: "+e.Message());
    }
}
</pre></td></tr></table>
<h2><a name="KeyType"><h2>KeyType&lt;T&gt;</h2></a></h2>
<p>KeyType&lt;T&gt; is a class to tell the collections if the collection contains elements of type T, what type is proper for the parameter of query functions. For example, List&lt;Ptr&lt;T&gt&gt;::Contains function take a parameter of type T* because if the function take a parameter of type Ptr&lt;T&gt;, the mistake of casting a pointer to a smart point twice is disappeared from the syntax error list. </p>
<p>The default evaluator of KeyType is</p>
<table id = "code"><tr><td><pre>
template&lt;typename T&gt;
struct KeyType
{
public:
    typedef T Type;
};
</pre></td></tr></table>
<p>For the reason that is mentioned above, the KeyType evaluator of a smart pointer is</p>
<table id = "code"><tr><td><pre>
template&lt;typename T&gt;
struct KeyType&lt;Ptr&lt;T&gt;&gt;
{
    typedef T* Type;
};
</pre></td></tr></table>
<p>You can defined your own KeyType evaluator overloading if your class has a similar problem with the smart pointer.</p>
<table id = "code"><tr><td><pre>
List&lt;Ptr&lt;A&gt;&gt; as;
A* a = new A;
as.Add(a);//convert from A* to Ptr&lt;A&gt; and the object will be deleted when the as is being destroyed.
as.Contains(a);//if the function Contains take a parameter of type of Ptr&lt;A&gt;, when the function finished, the object a is deleted and it will crashes when the as is trying to delete it again.
</pre></td></tr></table>
<h2><a name="POD">POD&lt;T&gt;</a></h2>
<p>POD is a type evaluator for telling whether a instance of a type can be copied by memcpy. You can define your POD overloading if your class is something like the <b>plain old data</b></p>
<table id="code"><tr><td><pre>
#include "Library\Basic.h"
using namespace vl;

template&lt;/*you can put your type parameter here*/&gt;
struct POD&lt;YourClass&gt;
{
    static const bool Result = true/*or false*/;
};
</pre></td></tr></table>
<h2><a name="Interface">Interface</a></h2>
<p>Interface class is the base class of all interface class in the core library. Although interface is not a concept in C++, but the core library still defines many interfaces.</p>
<p>Interface class inherits NotCopyable, so any interface instance can not be copied by default. An interface class in the core library should satisfy the following constrains:</p>
<li>All base classes should be interface classes.</li>
<li>If the interface inherits from Interface, Interface should be the only base class.</li>
<li>Contains only publie abstract functions.</li>
<li>Should have a prefix "I" of the class name.</li>
<p>Interface class has a virtual destructor. So any interface class does not necessary to inherits Object.</p>
<table id="code"><tr><td><pre>
#include "Library\Basic.h"
using namespace vl;

class IInterfaceA : public Interface
{
public:
    virtual void MethodA()=0;
};

class IInterfaceB : public Interface
{
public:
    virtual void MethodB()=0;
};

class IInterfaceX : public IInterfaceA, public IInterfaceB
{
};
</pre></td></tr></table>
<h2><a name="Event">Event</a></h2>
<p>Event class is a Func collection. Usually, Event is an implementation of Observer Pattern. When you need to expose an event, you can use Event and enable users to observe your object by binding their Func into your Event.</p>
<p>Event has three members: Add, Remove and operator(). Add is for binding Func into Event. Remove is for unbinding Func into Event. operator() is for invokeing every Func in a Event.</p>
<p>Event does not allow to bind a function which does not return void.</p>
<table id="code"><tr><td><pre>
#include "Library\Basic.h"
#include "Library\String.h"
using namespace vl;

void PrintMe1(const WString& name)
{
    Console::WriteLine(L"PrintMe1("+name+")");
}
void PrintMe2(const WString& name)
{
    Console::WriteLine(L"PrintMe1("+name+")");
}
void PrintMe3(const WString& name)
{
    Console::WriteLine(L"PrintMe1("+name+")");
}

int main()
{
    Event&lt;void(const WString&)&gt; event;
    event.Add(PrintMe1);
    event.Add(PrintMe2);
    event.Add(PrintMe3);
    event(L"vczh");
    event.Remove(PrintMe1);
    event.Remove(PrintMe2);
    event(L"vczh");
    return 0;
}
</pre></td></tr></table>
<h2><a name="Ptr">Ptr&lt;T&gt;</a></h2>
<p>Smart pointer Ptr&lt;T&gt; can store a T* object and decide where to delete that object. Ptr&lt;T&gt; uses a reference counter to know how many Ptr&lt;T&gt; links to the same T* object.</p>
<p>Comparing Ptr&lt;T&gt; is the same to comparing the T* object inside it. Ptr&lt;T&gt; has a member T* Obj()const to get the real object inside the Ptr&lt;T&gt;.</p>
<table id="code"><tr><td><pre>
#include "Library\Basic.h"
#include "Library\Pointer.h"
using namespace vl;

Ptr&lt;MyClass&gt; pa = new MyClass;
Ptr&lt;MyClass&gt; pb = pa;//CORRECT!!! Here pa and pb knows each other.
Ptr&lt;MyClass&gt; pc = pb.Obj();//WRONG!!! Here pc and {pa, pb} does not know each other. The MyClass* object will be delete twice.
</pre></td></tr></table>
</body>
</html>